package panda.log;

import java.util.Iterator;

import panda.file.Block;
import panda.file.Page;

public class LogIterator implements Iterator<BasicLogRecord> {
	private Page page = new Page();

	private Block block;

	private int currentRecord;
	
	public LogIterator(Block block){
		this.block = block;
		page.read(block);
		currentRecord = page.getInt(0);
	}
	
	
	public void remove() {
	}

	public boolean hasNext() {
		return currentRecord > 0 || block.hasNext();
	}

	public BasicLogRecord next() {
		if (currentRecord == 0)
			moveToNextBlock();
		currentRecord = page.getInt(currentRecord);
		return new BasicLogRecord(page, currentRecord + Page.INT_SIZE);
	}

	private void moveToNextBlock() {
		block = block.nextBlock();
		page.read(block);
		currentRecord = page.getInt(0);
	}
}
